package logic

import (
	"context"
	"taobao-merchandise/taobaorpc/taobaorpcclient"

	"taobao-merchandise/api/internal/svc"
	"taobao-merchandise/api/internal/types"

	"github.com/zeromicro/go-zero/core/logx"
)

type ProductUpdateLogic struct {
	logx.Logger
	ctx    context.Context
	svcCtx *svc.ServiceContext
}

func NewProductUpdateLogic(ctx context.Context, svcCtx *svc.ServiceContext) *ProductUpdateLogic {
	return &ProductUpdateLogic{
		Logger: logx.WithContext(ctx),
		ctx:    ctx,
		svcCtx: svcCtx,
	}
}

func (l *ProductUpdateLogic) ProductUpdate(req *types.ProductUpdateRequest) (resp *types.Response, err error) {
	// todo: add your logic here and delete this line
	update, err := l.svcCtx.TaoBaoRpc.ProductUpdate(l.ctx, &taobaorpcclient.ProductUpdateRequest{
		Title:     req.Title,
		UserId:    req.UserId,
		Price:     float32(req.Price),
		Stock:     req.Stock,
		Image:     req.Image,
		ProductId: req.ProductId,
	})
	if err != nil {
		return &types.Response{
			Message: err.Error(),
			Code:    0,
			Data:    nil,
		}, nil
	}
	return &types.Response{
		Message: "修改成功",
		Code:    200,
		Data:    update,
	}, nil
}
